package com.genersoft.iot.vmp.vmanager.record;

//RtpPacket.java


import static com.genersoft.iot.vmp.vmanager.record.CalculateUtil.intToByte;

class RtpPayloadType
{
    public static int RTP_PAYLOAD_TYPE_PCMU = 0;
    public static int RTP_PAYLOAD_TYPE_PCMA = 8;
}

public class RtpPacket
{

    private byte version = 2;

    private byte padding = 0;

    private byte extension = 0;

    private byte csrc_count = 0;

    private byte marker = 1;

    private byte playload_type = 0;

    private int sequence_number = 0;

    private long timestamp = 0;

    private long ssrc = 0;


    public RtpPacket(long ssrcVal) {
        ssrc = ssrcVal;
    }

    public byte[] packet_g711a(byte[] audioData, int audioLen) {

        int rtp_head_len = 12+2;
        byte[] payload = new byte[rtp_head_len + audioLen];

        playload_type = (byte) RtpPayloadType.RTP_PAYLOAD_TYPE_PCMA;
        sequence_number++;
        timestamp +=  audioLen;
        //包长
        System.arraycopy(intToByte(audioLen + 12), 0, payload, 0, 2);
        {
            byte temp = 0;
            temp = payload[0];
            payload[0] = payload[1];
            payload[1] = temp;
        }
        payload[0+2] = (byte)( (version << 6) +  (padding << 5) + (extension << 4) + csrc_count );
        payload[1+2] = (byte)( (marker << 7) + playload_type );
        payload[2+2] = (byte)( sequence_number / (0xff + 1) );
        payload[3+2] = (byte)( sequence_number % (0xff + 1) );
        payload[4+2] = (byte)( ( timestamp / (0xffff + 1) ) / (0xff + 1) );
        payload[5+2] = (byte)( ( timestamp / (0xffff + 1) ) % (0xff + 1) );
        payload[6+2] = (byte)( ( timestamp % (0xffff + 1) ) / (0xff + 1) );
        payload[7+2] = (byte)( ( timestamp % (0xffff + 1) ) % (0xff + 1) );
        payload[8+2] = (byte)( ( ssrc / (0xffff + 1) ) / (0xff + 1) );
        payload[9+2] = (byte)( ( ssrc / (0xffff + 1) ) % (0xff + 1) );
        payload[10+2] = (byte)( ( ssrc % (0xffff + 1) ) / (0xff + 1) );
        payload[11+2] = (byte)( ( ssrc % (0xffff + 1) ) % (0xff + 1) );

        System.arraycopy(audioData, 0, payload, rtp_head_len, audioLen);
        return payload;
    }


}

